package org.mech.sefer.client.ui.controller;

import java.util.List;

import javax.annotation.Resource;

import org.mech.sefer.client.ui.event.BibleEvents;
import org.mech.sefer.client.ui.event.BibleEvents.BibleRequestEvent;
import org.mech.sefer.client.ui.event.BibleEvents.BibleRequestHandler;
import org.mech.sefer.client.ui.spring.task.TaskService;
import org.mech.sefer.client.ui.task.callback.DefaultTaskCallback;
import org.mech.sefer.model.bible.TBible;
import org.mech.sefer.service.bible.BibleService;
import org.springframework.stereotype.Component;

@Component("bibleController")
public class BibleController extends Controller implements BibleRequestHandler {

//	@Resource(name = "bibleService")
//	private BibleService bibleService;

	@Resource(name = "taskService")
	private TaskService service;

	@Override
	protected void onRegister() {
		getEventBus()
				.registerHandler(BibleEvents.BibleRequestEvent.class, this);
	}

	@Override
	public void onBibleRequestEvent(BibleRequestEvent event) {

		service.call(BibleService.class, "loadAll",
				new DefaultTaskCallback<List<TBible>>(getEventBus()) {

					@Override
					public void onSuccess(List<TBible> result) {
						fireEvent(new BibleEvents.BibleAvailableEvent(result));
					}
				});
	}

}
